#!/bin/bash

repository=${OCROREP:-ssh://define.me/ocropus}
dirs="iulib llpy ocrofst ocrolseg ocropy ocrorast pyopenfst"
default_download=${OCRODOWNLOAD:-http://www-user.rhrk.uni-kl.de/~tmb}
default_cmodels="uw3unlv-240-4-60-g2.cmodel uw3unlv-240-8-40-nogeo.cmodel space.model space2.model default.fst none.fst case.fst" 
default_cmodel=uw3unlv-240-8-40-nogeo.cmodel
datadir=/usr/local/share/ocropus
jobs="" # "-j 4" # number of make jobs to run in parallel

die() {
    echo ERROR "$*"
    exit 1
}

if test "$1" = ""; then

cat <<EOF

Run the following in sequence in order to install OCRopus:

	sudo $0 packages    -- install required packages
	$0 install          -- compile and install everything
	sudo $0 dl          -- download models

Other commands:

	sudo $0 cleanup     -- removes most of the build directories/files
	sudo $0 uninstall   -- removes most of the installed files from /usr/local

EOF
exit 0

fi

cmd=$1
shift
echo === $cmd
case "$cmd" in
hg)
    for dir in $dirs; do
        echo "=== cd $dir; hg $*"
            (cd $dir && hg "$@") ||
                (echo $dir: error; exit 1)
    done
    ;;

clone)
    for dir in $dirs; do
        echo === cloning $dir
            hg clone $repository/$dir || 
                (echo $dir: error cloning; exit 1)
    done
    ;;

pull)
    for dir in $dirs; do
        echo === pulling $dir
            (cd $dir && hg pull $repository/$dir && hg update) || 
                (echo $dir: error pulling; exit 1)
    done
    ;;

push)
    for dir in $dirs; do
        echo === pushing $dir
        (cd $dir && hg push $repository/$dir) || 
            (echo $dir: error pushing; exit 1)
    done
    ;;

commit)
    for dir in $dirs; do
        echo === committing $dir
            (cd $dir && hg commit "$@") || 
                (echo $dir: error committing; exit 1)
    done
    ;;

packages)
    # do not depend on specific version of libjpeg
    # http://lists.debian.org/debian-devel-announce/2010/02/msg00006.html
    sudo apt-get install \
        build-essential imagemagick scons mercurial \
        libpng-dev libjpeg-dev libtiff-dev libgif-dev \
        libsdl-gfx1.2-dev libsdl-image1.2-dev libsdl1.2-dev \
        libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
        libavfilter-dev \
        libgsl0-dev libblas-dev \
        libsqlite3-dev\
        liblapack-dev libatlas-base-dev \
        python-dev swig2.0 zlib1g-dev \
        python-scipy python-matplotlib python-matplotlib-data \
        python-scikits-learn \
        python-tables \
        ipython curl cmake
        # tesseract-ocr tesseract-ocr-dev
        # tesseract-ocr-dev libleptonica-dev
    ;;


cleanup)
    rm -rf flann*/build
    rm -rf flann*/src/python/build
    rm -rf iuprlab/build
    rm -rf ocrofst/build
    rm -rf ocrolseg/build
    rm -rf ocropy/build
    rm -rf ocrorast/build
    rm -rf pyopenfst/build

    for dir in $dirs; do
        echo $dir
        (cd $dir && python setup.py clean)
        (cd $dir && test -f SConstruct && scons -c)
        (cd $dir && test -f Makefile && make clean)
    done
    ;;

install)
    set -o errexit

    echo please sudo now so that installs work
    sudo id

    # compile iulib

    (
    cd iulib
    scons -c
    sudo sh uninstall
#    sudo sh ubuntu-packages # should already have been done
    scons $jobs sdl=1
    sudo scons $jobs sdl=1 install
    cd pyswig
    python setup.py build_ext
    sudo python setup.py install
    )

    # compile ocrolseg

    (
    cd ocrolseg
    make
    sudo python setup.py install
    cd ..
    date;
    )

    # compile ocrorast

    (
    cd ocrorast
    make
    python setup.py build_ext
    sudo python setup.py install
    cd ..
    date;
    )

    # compile ocropy

    (
    cd ocropy
    sudo python setup.py install
    cd ..
    date;
    )

    # compile llpy

    (
    cd llpy
    sudo python setup.py install
    cd ..
    date;
    )

    # compile ocrofst 

    (
    cd ocrofst
    (cd ocrofstll && scons && sudo scons install)
    python setup.py build_ext
    sudo python setup.py install
    cd ..
    date;
    )

    # compile openfst 

    (
    rm -rf openfst-1.1
    test -f DIST/openfst-1.1.tar.gz && tar -zxvf DIST/openfst-1.1.tar.gz 
    cd openfst-1.1
    ./configure
    make $jobs
    sudo make install
    cd ..
    date;
    )

    # compile Python bindings for openfst

    (
    cd pyopenfst
    make
    cd ..
    date;
    )

    # optionally compile FLANN

    (
    sudo rm -rf flann-1.7.1-src
    unzip DIST/flann-1.7.1-src.zip
    cd flann-1.7.1-src
    rm -rf build
    mkdir build
    cd build
    cmake .. -DBUILD_MATLAB_BINDINGS=OFF
    make
    sudo make install
    )


    # last messages to the user

    (echo $LD_LIBRARY_PATH | grep /usr/local/lib) || {
            echo
            echo IMPORTANT
            echo
            echo You must add /usr/local/lib/ to your LD_LIBRARY_PATH variable 
            echo and add /usr/local/bin to your PATH variable.
            echo
    }

    ;;

dl)
    (
    mkdir -p $datadir
    cd $datadir || die "can't cd to $datadir"
    rm -f default.cmodel
    for m in $default_cmodels; do
        echo $m
        if test -f $m; then echo ... exists; continue; fi
        curl $default_download/$m.bz2 | bunzip2 > $m || rm -f $m
    done
    echo linking default.cmodel
    test -e default.cmodel || ln -s $default_cmodel default.cmodel || die "no default model"
    )
    ;;

tests)
    ocropus ocropy/tests/testpage.png
    ;;

uninstall)
    prefix=/usr/local
    rm -rfv ${prefix}/include/colib
    rm -rfv ${prefix}/include/iulib
    rm -rfv ${prefix}/include/ocropus
    rm -rfv ${prefix}/include/ocrorast
    rm -rfv ${prefix}/include/ocrolseg
    rm -rfv ${prefix}/include/ocrofst
    rm -rfv ${prefix}/lib/libiulib*
    rm -rfv ${prefix}/lib/libocropus*
    rm -rfv ${prefix}/lib/libocrorast*
    rm -rfv ${prefix}/lib/libocrofst*
    rm -rfv ${prefix}/lib/python*/dist-packages/_ocro*.so
    rm -rfv ${prefix}/lib/python*/dist-packages/ocro*
    ;;

*)
    echo "unknown command:" $1
    exit 1
    ;;

esac

